package com.runa.rccprotocol.dto.qiyang;

import com.runa.protocol.dto.IResponseData;
import com.runa.protocol.enums.ByteOrder;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.dto.ConcentratorRequestData;

/**
 * 2024/7/22 19:14
 * zhangYB
 */
public class QyRequestData extends ConcentratorRequestData {


    private byte[] data;
    private String message;
    private String serial;

    public QyRequestData(String address) {
        super(address);
    }

    @Override
    public byte[] getData() {
        return data;
    }

    @Override
    public boolean check(IResponseData data) {
        return true;
    }

    public String getMessage() {
        if (null != message) {
            return message;
        }
        message = ByteUtil.byteToHexString(getData());
        return message;
    }

    public void readMeter(String meterSerial) {
        this.serial = meterSerial;
        int index = 0;
        byte[] bs = new byte[14];
        bs[index++] = (byte)0xFE;
        bs[index++] = (byte)0xFE;
        bs[index++] = (byte)0x02;
        bs[index++] = (byte)0x03;
        ByteUtil.intToByte(Integer.valueOf(address), bs, index, ByteOrder.DCBA);

        index += 5;
        byte[] serialBytes = ByteUtil.hexStringToByteFlip(serial);
        for (byte serialByte : serialBytes) {
            bs[index++] = serialByte;
        }
        byte sum = 0;
        for (int i = 2; i < index; i++) {
            sum = (byte)((sum + bs[i]) & 0xff);
        }
        bs[index] = sum;

        byte[] bytes = new byte[bs.length * 2];
        int cnt = 0;
        for (byte b : bs) {
            if (b == (byte) 0xfd) {
                bytes[cnt++] = (byte) 0xfd;
                bytes[cnt++] = (byte) 0xed;
            } else if (b == (byte) 0xfe) {
                bytes[cnt++] = (byte) 0xfd;
                bytes[cnt++] = (byte) 0xee;
            } else {
                bytes[cnt++] = b;
            }
        }
        data = new byte[cnt];
        System.arraycopy(bytes, 0, data, 0, cnt);


        setHaveResult(true);
    }

    public void readValve(String valveSerial) {
        this.serial = valveSerial;
        int index = 0;
        byte[] bs = new byte[14];
        bs[index++] = (byte)0xFE;
        bs[index++] = (byte)0xFE;
        bs[index++] = (byte)0x02;
        bs[index++] = (byte)0x13;
        ByteUtil.intToByte(Integer.valueOf(address), bs, index, ByteOrder.DCBA);

        index += 5;
        byte[] serialBytes = ByteUtil.hexStringToByteFlip(serial);
        for (byte serialByte : serialBytes) {
            bs[index++] = serialByte;
        }
        byte sum = 0;
        for (int i = 2; i < index; i++) {
            sum = (byte)((sum + bs[i]) & 0xff);
        }
        bs[index] = sum;
        data = encode(bs);
        setHaveResult(true);
    }


    private byte[] encode(byte[] bs) {
        byte[] bytes = new byte[bs.length * 2];
        int cnt = 0;
        for (int i = 0; i < bs.length; i++) {
            if (bs[i] == (byte) 0xfd) {
                bytes[cnt++] = (byte) 0xfd;
                bytes[cnt++] = (byte) 0xed;
            } else if (bs[i] == (byte) 0xfe) {
                bytes[cnt++] = (byte) 0xfd;
                bytes[cnt++] = (byte) 0xee;
            } else {
                bytes[cnt++] = bs[i];
            }
        }
        byte[] r = new byte[cnt];
        System.arraycopy(bytes, 0, r, 0, cnt);
        return r;
    }

    public void valveControlOpen(String valveSerial, boolean open) {
        byte[] bs = new byte[100];
        int index = 0;
        bs[index++] = (byte)0xFE;
        bs[index++] = (byte)0xFE;
        bs[index++] = (byte)0x08;
        bs[index++] = (byte)0x01;
        ByteUtil.intToByte(Integer.valueOf(address), bs, index, ByteOrder.DCBA);
        index += 4;
        int lenIndex = index++;
        byte[] controlValve = createControlValve(valveSerial, open);

        bs[lenIndex] = (byte)(controlValve.length & 0xFF);
        for (byte b : controlValve) {
            bs[index++] = b;
        }
        bs[index++] = ByteUtil.sum(bs, 2, index);
        byte[] r = new byte[index];
        System.arraycopy(bs, 0, r, 0, index);
        System.out.println("控阀：" + ByteUtil.byteToHexString(r));
        data = encode(r);
        setHaveResult(true);
    }

    public byte[] createControlValve(String valveSerial, boolean open) {
        byte[] bs = new byte[100];
        int index = 0;
        bs[index++] = (byte)0xFE;
        bs[index++] = (byte)0xFE;
        bs[index++] = (byte)0x68;
        bs[index++] = (byte)0x49;
        if (valveSerial.length() % 2 == 1) {
            valveSerial = "0" + valveSerial;
        }
        byte[] serialBytes = ByteUtil.hexStringToByteFlip(valveSerial);
        for (byte serialByte : serialBytes) {
            bs[index++] = serialByte;
        }
        bs[index++] = (byte)0x00;
        bs[index++] = (byte)0x29;
        bs[index++] = (byte)0x0F;
        bs[index++] = (byte)0x04;
//        bs[index++] = (byte)0x05;
        bs[index++] = (byte)0x04;
        bs[index++] = (byte)0xA0;
//        bs[index++] = (byte)0x06;
        bs[index++] = (byte)0x17;
        bs[index++] = (byte)0x00;
        bs[index++] = open ? (byte)0x55 : (byte)0x99;
        bs[index++] = ByteUtil.sum(bs, 2, index -2);
        bs[index++] = (byte)0x16;
        byte[] r = new byte[index];
        System.arraycopy(bs, 0, r, 0, index);
        return r;
    }



    public static void main(String[] args) {
        QyRequestData qyRequestData = new QyRequestData("20190011");
        qyRequestData.readMeter("18007657");
        System.out.println("抄表：" + ByteUtil.byteToHexString(qyRequestData.getData()));
//        qyRequestData.readValveNo();
//        qyRequestData.readValveAll();
        qyRequestData.readValve("18006995");
        System.out.println("抄阀：" + ByteUtil.byteToHexString(qyRequestData.getData()));

        QyRequestData valveControl = new QyRequestData("20190011");
        valveControl.valveControlOpen("18006995", true);
        System.out.println(valveControl.getAddress() + "控阀：" + ByteUtil.byteToHexString(valveControl.getData()));


//        valveControl = new QyRequestData("20111040");
//        valveControl.valveControlOpen("87654321", true);
//        byte[] data1 = valveControl.getData();
//        System.out.println("控阀：" + ByteUtil.byteToHexString(data1));
    }


}
